package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.CwKjkm;
import cn.stylefeng.guns.sys.modular.system.entity.CwZykm;
import cn.stylefeng.guns.sys.modular.system.model.params.CwZykmParam;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdService;
import cn.stylefeng.guns.sys.modular.system.service.CwKjkmService;
import cn.stylefeng.guns.sys.modular.system.service.CwZykmService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 财务摘要科目
 *
 * @author gyz
 * @Date 2020-11-02 13:43:19
 */
@Controller
@RequestMapping("/cwZykm")
public class CwZykmController extends BaseController {

    private String PREFIX = "/modular/system/cwZykm";

    @Autowired
    private CwZykmService cwZykmService;
    @Autowired
    private CwKjkmService cwKjkmService;
    @Autowired
    private BtyBxdService btyBxdService;

    /**
     * 跳转到主页面
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/cwZykm.html";
    }

    /**
     * 新增页面
     *
     * @author gyz
     * @Date 2020-11-02
     */
   /* @RequestMapping("/add")
    public String add() {
        return PREFIX + "/cwKjkm_add.html";
    }*/

    /**
     * 编辑页面
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/cwZykm_edit.html";
    }

    /**
     * 新增接口
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(CwZykmParam param) {
        if(ToolUtil.isEmpty(param.getBtyCwKjkmId())){
            return ResponseData.error("会计科目不能为空");
        }
        CwZykm bySpec = this.cwZykmService.findBySpec(param);
        if(ToolUtil.isNotEmpty(bySpec)){
            return ResponseData.error("摘要科目名称已存在");
        }
        this.cwZykmService.add(param);
        return ResponseData.success();
    }

   /* *//**
     * 编辑接口
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(CwZykmParam param) {
        CwZykmParam p = new CwZykmParam();
        p.setName(param.getName());
        p.setBtyCwKjkmId(param.getBtyCwKjkmId());
        CwZykm bySpec = this.cwZykmService.findBySpec(p);
        if(ToolUtil.isNotEmpty(bySpec) && bySpec.getId().longValue()!=param.getId().longValue()){
            return ResponseData.error("摘要科目名称已存在");
        }
        this.cwZykmService.update(param);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(CwZykmParam param) {
        //查询是否使用中,禁止删除
        CwZykm km = cwZykmService.getById(param.getId());
        boolean b = cwZykmService.queryBxds(param);
        if(b){
            return ResponseData.error("存在关联摘要的报销单，不允许删除");
        }
        this.cwZykmService.delete(param);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(CwZykmParam param) {
        CwZykm detail = this.cwZykmService.getById(param.getId());
        CwKjkm kjkm = cwKjkmService.getById(detail.getBtyCwKjkmId());
        detail.setBtyCwKjkmName(kjkm.getName());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author gyz
     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(CwZykmParam param) {
        return this.cwZykmService.findPageBySpec(param);
    }

}


